package cc.vicp.djx314.starfood.service.account.impls;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cc.vicp.djx314.starfood.dao.account.AccountMapper;
import cc.vicp.djx314.starfood.dao.account.UserMapper;
import cc.vicp.djx314.starfood.domain.account.User;
import cc.vicp.djx314.starfood.service.account.AccountService;
import cc.vicp.djx314.starfood.service.account.UserInfoService;
import cc.vicp.djx314.starfood.service.exception.UserNotFoundException;

@Service
public class UserInfoServiceImpl implements UserInfoService {
	public static final Logger logger = LoggerFactory.getLogger(UserInfoServiceImpl.class);

	@Autowired
	UserMapper userMapper;

	@Autowired
	AccountMapper accountMapper;

	@Override
	@Transactional(readOnly = true)
	public Resource getUserHeadImg(Integer id) throws UserNotFoundException {
		Resource userImg = userMapper.getUserHeadImg(id);
		if (userImg == null) {
			throw new UserNotFoundException("id为：" + id + "的用户找不到");
		} else {
			return userImg;
		}
	}

	@Override
	@Transactional(readOnly = true)
	public boolean isNickNameExists(String nickName) {
		boolean isExists = true;
		try {
			Integer id = userMapper.isNickNameExists(nickName);
			if (id == null) {
				isExists = false;
			}
		} catch (Exception e) {
			logger.info("数据库发生未知错误", e);
		}
		return isExists;
	}

	@Override
	@Transactional(readOnly = false)
	public void createUser(User user, byte[] headImg) throws Exception {
		accountMapper.createAccount(user.getAccount());
		userMapper.createUser(user, headImg);
		if (user.getId() == null) {
			throw new Exception("新建用户发生错误,数据没有预期插入");
		}
		Integer roleId = accountMapper.getRoleId(AccountService.ROLE_USER);
		int rows = userMapper.createUserRole(user.getAccount(), roleId);
	}

	@Override
	@Transactional(readOnly = true)
	public User getUserInfo(String accountName) throws UserNotFoundException, DataAccessException {
		User user = userMapper.getUserInfo(accountName);
		if (user == null) {
			throw new UserNotFoundException("账号为" + accountName + "的user找不到");
		}
		return user;
	}

	@Override
	@Transactional(readOnly = true)
	public User getAllUserInfo(Integer userId) throws UserNotFoundException {
		User user = userMapper.getAllUserInfo(userId);
		if (user == null) {
			throw new UserNotFoundException("id为:" + userId + "的用户找不到");
		}
		return user;
	}

	@Override
	@Transactional(readOnly = false)
	public void updateUserInfo(User user) throws Exception {
		int rows = userMapper.updateUserInfo(user);
		if (rows == 0) {
			throw new Exception("修改用户信息过程出错,没有行数的变动");
		}
	}

	@Override
	@Transactional(readOnly = true)
	public boolean getIfOtherUserNickNameExists(User user) {
		Integer sum = userMapper.getIfOtherUserNickNameExists(user);
		boolean isExists = false;
		if (sum != null && sum > 0) {
			isExists = true;
		}
		return isExists;
	}

	@Override
	public void updateUserPhoto(Integer userId, byte[] imageFile) throws Exception {
		int rows = userMapper.updateUserPhoto(userId, imageFile);
		if (rows != 1) {
			throw new Exception("更新id为:" + userId + "的用户的图像修改的行数大于一行");
		}
	}
}